"""
@project: PyCharm
@author: MZM
@file: reader.py
@ide: PyCharm
@time: 2021/6/30 15:08
@desc：
"""
import os
import socket
import sys

from ruamel.yaml import YAML
from httprunner import Parameters

class Reader:
    @classmethod
    def read_case(cls,filepath):
        '''
        :param filepath:
        :return:
        '''
        with open(filepath, 'r', encoding='utf-8') as f:
            yaml = YAML(typ='unsafe', pure=True)
            r =  yaml.load(f)
            suitelist = []
            for ts in r:
                if 'parameters' in ts.keys() and ts['parameters'] != {}:
                    params = Parameters(ts['parameters'])
                    for p in params:
                        casedict = {}
                        casedict = ts.copy()
                        casedict['parameters'] = p
                        suitelist.append(casedict)
                else:
                    suitelist.append(ts)
        return suitelist

    @classmethod
    def read_ids(cls, filepath):
        suitelist = cls.read_case(filepath)
        ids_list = []
        for data in suitelist:
            ids_list.append(data['config']['name'])
        return ids_list

    @classmethod
    def read_casedata(cls,filepath):
        '''
        :param filepath:
        :return:
        '''
        with open(filepath, 'r', encoding='utf-8') as f:
            yaml = YAML(typ='unsafe', pure=True)
            r =  yaml.load(f)
        return r

if __name__ == "__main__":
    ip = socket.gethostbyname(socket.gethostname())
    print(Reader.read_case(f'./data/test.yaml'))